<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="票名称">
          <el-input v-model="form.name" placeholder="请输入票名称"></el-input>
        </el-form-item>
        <el-form-item label="票分类">
          <el-select v-model="form.type" placeholder="选择票类型">
            <el-option
              v-for="(item, index) in Data"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="form.time"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            @change="timeChange"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="display: flex; justify-content: flex-end">
      <div class="salesAmount">销售金额：{{ money }}元</div>
    </div>
    <div>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          :index="Method"
          type="index"
          prop="id"
          width="55"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="water_name" align="center" label="票名称">
        </el-table-column>
        <el-table-column prop="goods_cat_name" align="center" label="票分类">
        </el-table-column>

        <el-table-column prop="unit" align="center" label="票单位">
        </el-table-column>
        <!-- <el-table-column align="center" label="水票图片">
          <template slot-scope="scope">
            <el-image
              style="width: 80px; height: 80px"
              :src="scope.row.water_img"
              fit="cover"
            ></el-image>
          </template>
        </el-table-column> -->
        <el-table-column prop="price" align="center" label="票单价">
        </el-table-column>
        <el-table-column prop="sellingCount" align="center" label="票售卖数量">
        </el-table-column>
        <el-table-column prop="sellingMoney" align="center" label="售卖总价">
        </el-table-column>
        <el-table-column prop="ctime" align="center" label="统计日期">
          <template slot-scope="scope">
            {{
              scope.row.ctime
                .split(",")
                .map((date) => date.slice(0, 10))
                .join(",")
            }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              size="mini"
              type="primary"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="packGit"
        border
        style="width: 100%"
      >
        <!-- <el-table-column
          label="序号"
          type="index"
          width="55"
          :index="Method"
          align="center"
        >
        </el-table-column> -->
        <el-table-column prop="order_id" align="center" label="订单编号">
        </el-table-column>
        <el-table-column prop="name" align="center" label="商品名称">
        </el-table-column>
        <el-table-column prop="ctime" align="center" label="下单日期">
        </el-table-column>
        <el-table-column prop="nickname" align="center" label="用户名称">
        </el-table-column>
        <el-table-column prop="user_id" align="center" label="用户编号">
        </el-table-column>
        <el-table-column prop="nums" align="center" label="商品数量">
        </el-table-column>
        <el-table-column prop="amount" align="center" label="金额">
        </el-table-column>
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"
          :current-page="page1"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total1"
        >
        </el-pagination>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  packageCount,
  packageIndex,
  classification,
} from "../../../../utils/api.js";
import { shuiPiaoList, shuiPiaoDetail } from "../../../../utils/main";
export default {
  data() {
    return {
      Data: [
        {
          name: "水票",
          id: "1",
        },
      ],
      money: "",
      token: sessionStorage.getItem("token"),
      //请求特效
      loading: false,
      //弹窗
      dialogVisible: false,
      form: {
        name: "",
        type: "",
        time: "",
      },
      time: "",
      //发送请求时间参数
      //展示数据
      tableData: [
        // {
        //   water_name: "",
        //   goods_cat_name: "",
        //   water_number: "",
        //   id: "",
        //   brand_name: "",
        //   goods_name: "",
        //   water_img: "",
        //   ctime: "",
        //   buy_count: "",
        //   water_cat_id: "",
        //   ctime: "",
        // },
      ],
      //明细
      packGit: [
        //   {
        //     water_nameL: "",
        //     goods_cat_name: "",
        //     package_nember: "",
        //     goods_name: "",
        //     brand_name: "",
        //     water_img: "",
        //     ctime: "",
        //     payed: "",
        //   },
      ],
      //水票类型
      // Data: [
      //   {
      //     id: "",
      //     name: "",
      //   },
      // ],
      page: 1,
      limit: 10,
      total: 0,
      page1: 1,
      limit1: 10,
      total1: 0,
      name: "",
      type: "",

      order_id: 0,
    };
  },
  methods: {
    //日期
    timeChange(e) {
      // console.log(e);
      // let list = [];
      // e.forEach((item) => {
      //   let date = new Date(item);
      //   let year = date.getFullYear();
      //   let month = date.getMonth() + 1;
      //   let day = date.getDate();
      //   let h = date.getHours();
      //   let min = date.getMinutes();
      //   let s = date.getSeconds();
      //   month = month < 10 ? "0" + month : month;
      //   day = day < 10 ? "0" + day : day;
      //   h = h < 10 ? "0" + h : h;
      //   min = min < 10 ? "0" + min : min;
      //   s = s < 10 ? "0" + s : s;
      //   list.push(
      //     year + "-" + month + "-" + day + " " + h + ":" + min + ":" + s
      //   );
      // });

      // this.time = list[0] + "," + list[1];
      // console.log(this.time);
      this.time = this.form.time;
      if (this.form.time == null) {
        this.time = "";
      }
      this.getList();
    },
    //查询
    onSubmit(row) {
      this.name = this.form.name;
      this.time = this.form.time;
      if (this.time == null) {
        this.time = "";
      }
      this.type = this.form.type;
      this.page = 1;
      this.getList();
    },
    onReset() {
      this.name = "";
      this.time = "";
      this.type = "";
      this.form.name = "";
      this.form.time = "";
      this.form.type = "";
      this.getList();
    },
    //水票分类
    // selectChange() {},
    //分页器
    handleSizeChange(val) {
      this.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    },
    handleSizeChange1(val) {
      this.limit1 = val;
      this.shuiPiaoDetail(this.order_id);
    },
    handleCurrentChange1(val) {
      this.page1 = val;
      this.shuiPiaoDetail(this.order_id);
    },
    // 序号排序
    Method(index) {
      if (this.token < this.limit) {
        return this.total - index;
      } else {
        return this.total - (this.page - 1) * this.limit - index;
      }
    },
    //商品分类
    sdhadj() {
      this.loading = true;
      classification(this.token)
        .then((res) => {
          if (res.data.status) {
            let list = res.data.data.data;
            this.Data = list.filter((e) => {
              return e.name != "套餐热销" && e.name != "热销";
            });
            let timer = setTimeout(() => {
              this.loading = false;
              clearTimeout(timer);
            }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    // 获取水票出售列表
    async getList() {
      this.loading = true;
      let res = await shuiPiaoList(
        this.token,
        this.page,
        this.limit,
        this.name,
        this.type,
        this.time
      );
      if (res.data.status) {
        this.tableData = res.data.data.data || [];
        this.total = res.data.data.count;
        this.money = res.data.data.sum;
        this.tableData.forEach((item) => {
          if (item.order_items != null && item.bill_aftersales_itemss == null) {
            item.sellingCount = item.order_items.nums;
            item.sellingMoney = item.order_items.amount;
          } else if (
            item.order_items != null &&
            item.bill_aftersales_itemss != null
          ) {
            item.sellingCount =
              item.order_items.nums - item.bill_aftersales_itemss.nums;
            item.sellingMoney =
              item.order_items.amount - item.bill_aftersales_itemss.amount;
          } else if (
            item.order_items == null &&
            item.bill_aftersales_itemss == null
          ) {
            item.sellingCount = "0";
            item.sellingMoney = "0.00";
          }
        });
        // console.log(this.total, shouchu);
        let timer = setTimeout(() => {
          this.loading = false;
          clearTimeout(timer);
        }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/login");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },
    //获取列表数据
    // packageCounts() {
    //   this.loading = true;
    //   packageCount(
    //     this.token,
    //     this.page,
    //     this.limit,
    //     this.form.mobile,
    //     this.time,
    //     this.form.name
    //   )
    //     .then((res) => {
    //       if (res.data.status) {
    //         this.tableData = res.data.data.data;
    //         this.total = res.data.data.count;
    //         this.tableData.forEach((item) => {
    //           if (item.water_img) {
    //             item.water_img1 = item.water_img;
    //             item.water_img = "https://bjsf.bjsky.top" + item.water_img;
    //           }
    //         });
    //         let timer = setTimeout(() => {
    //           this.loading = false;
    //           clearTimeout(timer);
    //         }, 400);
    //       } else {
    //         if (res.data.msg == "token失效,请重新进行登录") {
    //           this.$router.push("/login");
    //         }
    //         if (res.data.msg == "此账号没有权限") {
    //           this.$message.error(res.data.msg);
    //           this.loading = false;
    //         }
    //         let timer = setTimeout(() => {
    //           this.$message.error(res.data.msg);
    //         }, 5000);
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err.message);
    //     });
    // },
    //明细列表数据
    // packageIndexs() {
    //   this.loading = true;
    //   packageIndex(this.token, this.page1, this.limit1, this.tableData.id, "")
    //     .then((res) => {
    //       if (res.data.status) {
    //         this.packGit = res.data.data.date;
    //         this.total1 = res.data.data.count;
    //         console.log(this.total1);
    //         this.packGit.forEach((item) => {
    //           if (item.water_img) {
    //             item.water_img1 = item.water_img;
    //             item.water_img = "https://bjsf.bjsky.top" + item.water_img;
    //           }
    //         });
    //         let timer = setTimeout(() => {
    //           this.loading = false;
    //           clearTimeout(timer);
    //         }, 400);
    //       } else {
    //         if (res.data.msg == "token失效,请重新进行登录") {
    //           this.$router.push("/login");
    //         }
    //         if (res.data.msg == "此账号没有权限") {
    //           this.$message.error(res.data.msg);
    //           this.loading = false;
    //         }
    //         let timer = setTimeout(() => {
    //           this.$message.error(res.data.msg);
    //         }, 5000);
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err.message);
    //     });
    // },
    //水票分类
    // sdhadj() {
    //   this.loading = true;
    //   classification(this.token)
    //     .then((res) => {
    //       if (res.data.status) {
    //         this.Data = res.data.data.data;

    //         let timer = setTimeout(() => {
    //           this.loading = false;
    //           clearTimeout(timer);
    //         }, 400);
    //       } else {
    //         if (res.data.msg == "token失效,请重新进行登录") {
    //           this.$router.push("/login");
    //         }
    //         if (res.data.msg == "此账号没有权限") {
    //           this.$message.error(res.data.msg);
    //           this.loading = false;
    //         }
    //         let timer = setTimeout(() => {
    //           this.$message.error(res.data.msg);
    //         }, 5000);
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err.message);
    //     });
    // },
    // 点击详情的事件
    handleClick(row) {
      // let obj = JSON.parse(JSON.stringify(row));
      // this.tableData.water_cat_id = obj.water_cat_id;
      // this.tableData.id = obj.id;
      // this.packageIndexs();
      this.dialogVisible = true;
      this.order_id = row.id;
      this.shuiPiaoDetail(this.order_id);
      return;
      this.$router.push(`/stn/shuipiaodetail?water_id=${row.water_id}`);
    },
    async shuiPiaoDetail(id) {
      let res = await shuiPiaoDetail(
        this.token,
        this.page1,
        this.limit1,
        id,
        this.form.time
      );
      if (res.data.status) {
        this.packGit = res.data.data.data || [];
        this.total1 = res.data.data.count;
        // console.log(this.total, shouchu);
        let timer = setTimeout(() => {
          clearTimeout(timer);
        }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/login");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },
    //关闭弹窗
    handleClose(done) {
      done();
      this.page1 = 1;
      // this.getList();
    },
  },
  mounted() {
    // this.packageCounts();
    // this.sdhadj();
    this.getList();
    this.sdhadj();
  },
};
</script>

<style scoped>
.salesAmount {
  margin-bottom: 10px;
  margin-right: 10px;
  font-size: 18px;
  color: rgba(249, 100, 34, 1);
}
</style>